Grokking Amazon Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Amazon Easy Problems
Redistribute Characters to Make All Strings Equal (easy)
Solution: Redistribute Characters to Make All Strings Equal
Longest Common Prefix (easy)
Solution: Longest Common Prefix
Make Array Zero by Subtracting Equal Amounts (easy)
Solution: Make Array Zero by Subtracting Equal Amounts
Leaf-Similar Trees (easy)
Solution: Leaf-Similar Trees
Path Crossing (easy)
Solution: Path Crossing
Special Array With X Elements Greater Than or Equal X (easy)
Solution: Special Array With X Elements Greater Than or Equal X
Relative Sort Array (easy)
Solution: Relative Sort Array
Transpose Matrix (easy)
Solution: Transpose Matrix
Find Common Characters (easy)
Solution: Find Common Characters
Fizz Buzz (easy)
Solution: Fizz Buzz
Increasing Order Search Tree (easy)
Solution: Increasing Order Search Tree
Largest Odd Number in String (easy)
Solution: Largest Odd Number in String
Best Time to Buy and Sell (easy)
Solution: Best Time to Buy and Sell Stock
Take Gifts From the Richest Pile(easy)
Solution: Take Gifts From the Richest Pile
Remove Duplicates from Sorted List (easy)
Solution: Remove Duplicates from Sorted List
Amazon Medium Problems
Maximum Length of Subarray With Positive Product (medium)
Solution: Maximum Length of Subarray With Positive Product
Largest Submatrix With Rearrangements (medium)
Solution: Largest Submatrix With Rearrangements
Count the Number of Good Subarrays (medium)
Solution: Count the Number of Good Subarrays
Minimum Suffix Flips (medium)
Solution: Minimum Suffix Flips
Minimum Number of Operations to Make Array Empty (medium)
Solution: Minimum Number of Operations to Make Array Empty
Next Greater Element II (medium)
Solution: Next Greater Element II
Make Sum Divisible by P (medium)
Solution: Make Sum Divisible by P
Rotting Oranges (medium)
Solution: Rotting Oranges
Minimum Deletions to Make Character Frequencies Unique (medium)
Solution: Minimum Deletions to Make Character Frequencies Unique
Minimum Adjacent Swaps to Make a Valid Array (medium)
Solution: Minimum Adjacent Swaps to Make a Valid Array
Reverse Integer (medium)
Solution: Reverse Integer
Maximize Distance to Closest Person (medium)
Solution: Maximize Distance to Closest Person
Split Linked List in Parts (medium)
Solution: Split Linked List in Parts
Maximum Difference Between Node and Ancestor (medium)
Solution: Maximum Difference Between Node and Ancestor
Deepest Leaves Sum (medium)
Solution: Deepest Leaves Sum
The kth Factor of n (medium)
Solution: The kth Factor of n
Coin Change II (medium)
Solution: Coin Change II
Sum of Subarray Minimums (medium)
Solution: Sum of Subarray Minimums
Valid Sudoku (medium)
Solution: Valid Sudoku
Longest Increasing Subsequence (medium)
Solution: Longest Increasing Subsequence
Next Permutation (medium)
Solution: Next Permutation
Subsets (medium)
Solution: Subsets
Combination Sum (medium)
Solution: Combination Sum
Number of Provinces (medium)
Solution: Number of Provinces
Number of Islands (medium)
Solution: Number of Islands
Constrained Subsequence Sum (hard)
Solution: Constrained Subsequence Sum
Number of Valid Subarrays (hard)
Solution: Number of Valid Subarrays
Count Vowels Permutation (hard)
Solution: Count Vowels Permutation
Concatenated Words (hard)
Solution: Concatenated Words
Minimum Replacements to Sort the Array (hard)
Solution: Minimum Replacements to Sort the Array
Count All Valid Pickup and Delivery Options (hard)
Solution: Count All Valid Pickup and Delivery Options
Length of the Longest Valid Substring (hard)
Solution: Length of the Longest Valid Substring
Count Array Pairs Divisible by K (hard)
Solution: Count Array Pairs Divisible by K
Word Ladder (hard)
Solution: Word Ladder
Path with Maximum Sum (hard)
Solution: Path with Maximum Sum
Solution: Make Sum Divisible by P

Problem Statement

Given an array nums containing the positive integers, remove the smallest subarray (maybe empty) from nums such that the sum of the remaining elements is divisible by p. You are not allowed to remove the whole array.

Return the length of the smallest subarray that you need to remove, or -1 if it's not possible.

A subarray is a contiguous sequence of elements in the array.

Examples

Example 1:

  • Input: nums = [5, 9, 7, 8], P = 5
  • Expected Output: 1
  • Justification: Removing the subarray [9] results in a new array [5, 7, 8] whose sum is 20, which is divisible by 5.

Example 2:

  • Input: nums = [6, 4, 3, 2, 7, 6, 2], P = 10
  • Expected Output: 0
  • Justification: We don't need to remove any element from the array, as the sum of all array elements is divisible by 10.

Example 3:

  • Input: nums = [8, 19, 4, 3], P = 10
  • Expected Output: 1
  • Justification: By removing the subarray [4], we get a new array [8, 19, 3] with a sum of 30, Which is divisible by 10.

Solution

To solve this problem, we adopt a two-pronged strategy focusing on prefix sums and hash mapping. Initially, we calculate the total sum of the array and determine the excess or deficit with respect to P required to make the sum divisible by P. Recognizing that removing a subarray to adjust the total sum to a multiple of P mirrors finding a segment whose sum equals this excess or deficit, we proceed to track prefix sums and their indices.

This approach is underpinned by the insight that if two prefix sums have the same remainder when divided by P, the segment between them contributes a sum that is a multiple of P. Consequently, by maintaining a hash map of the earliest occurrence of each remainder, we can efficiently identify the minimum length of a subarray that, when removed, leaves the array's sum divisible by P. This method is not only intuitive but also leverages the efficiency of hash maps for quick lookups, rendering it a potent solution for the problem at hand.

Step-by-Step Algorithm

  1. Initialize Variables:

    • Calculate the total sum of the array elements and find its remainder when divided by P (totalSum % P). This will help in identifying the excess amount that needs to be addressed by removing a subarray.
    • Initialize an empty hash map (remainderMap) to store the prefix sum remainders as keys and their corresponding indices as values. Insert a base case into the hash map with 0 as the key and -1 as the value. This represents the prefix sum before the array starts, facilitating scenarios where the required subarray starts from index 0.
    • Define variables for the current sum (currSum), set initially to 0, and the minimum length of the subarray to be removed (minLen), set initially to an arbitrarily large value (like Integer.MAX_VALUE in Java or float('inf') in Python).
  2. Iterate Through the Array:

    • For each element in the array, add it to currSum and calculate the remainder of currSum when divided by P.
    • Calculate the complement of the current remainder with respect to the total excess ((currSum - totalSum + P) % P). This step identifies what remainder we need to find in the hash map to form a continuous subarray whose removal could potentially make the total sum divisible by P.
  3. Update HashMap and Minimum Length:

    • If the complement calculated in the previous step exists in remainderMap, it means there exists a previous prefix sum that, when subtracted from the current prefix sum, results in a sum that is a multiple of P. Calculate the length of this subarray and update minLen if this length is smaller than the current minLen.
    • Update remainderMap with the current prefix sum remainder as the key and the current index as the value. This step is crucial for keeping track of the earliest occurrence of each remainder.
  4. Finalize Result:

    • After iterating through the entire array, check if minLen is still set to its initial large value or equals the length of the array. If so, return -1, indicating that it's not possible to remove any subarray to make the sum divisible by P.
    • Otherwise, return minLen as the minimum length of the subarray that needs to be removed.

Algorithm Walkthrough

Let's consider the input: nums = [5, 9, 7, 8], P = 5

  • Total Sum Calculation:

    • Total sum of [5, 9, 7, 8] is 29. 29 % 5 = 4. So, we need to adjust the total sum to be divisible by 5 by addressing this excess of 4.
  • Initialization:

    • remainderMap = {0: -1}
    • totalSum % P = 4, currSum = 0, minLen = inf
  • Iteration 1:

    • currSum with first element 5: currSum = (0 + 5) % 5 = 0
    • Complement to find: (0 - 4 + 5) % 5 = 1. Complement 1 doesn't exist in remainderMap.
    • remainderMap updated to {0: 0}
  • Iteration 2:

    • currSum with second element 9: currSum = (0 + 9) % 5 = 4
    • Complement to find: (4 - 4 + 5) % 5 = 0. Complement 0 exists in remainderMap.
    • Update minLen to 1 - 0 = 1.
    • remainderMap updated to {0: 0, 4: 1}
  • Iteration 3:

    • currSum with third element 7: currSum = (4 + 7) % 5 = 1
    • Complement to find: (1 - 4 + 5) % 5 = 2. Complement 2 does not exist in remainderMap.
    • remainderMap updated to {0: 0, 4: 1, 1: 2}
  • Iteration 4:

    • currSum with fourth element 8: currSum = (1 + 8) % 5 = 4
    • Complement to find: (4 - 4 + 5) % 5 = 0. Complement 0 exists in remainderMap.
    • minLen could be updated to 3 - 0 = 3, but since 1 < 3, minLen remains 1.
    • remainderMap final {0: 0, 4: 1, 1: 2}
  • Final Result:

    • The minimum length of the subarray that needs to be removed to make the sum divisible by P is 1. This corresponds to the removal of [9] to adjust the sum.

Code

Java
Saved to Cloud

. . . .

Complexity Analysis

  • Time Complexity: The algorithm iterates through the array once, making the time complexity , where N is the length of the array. The operations within each iteration (e.g., updating the hash map, calculating remainders) are constant time.
  • Space Complexity: The space complexity is also due to the hash map that stores up to N different remainders and their indices.
Make Sum Divisible by P (medium)
Rotting Oranges (medium)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-Step Algorithm

Algorithm Walkthrough

Code

Complexity Analysis